package teamdao

import (
	"itodo/core/dao/table"
	"itodo/core/localwrap"
	"itodo/core/service/common"

	"github.com/RocksonZeta/wrap/redised"
	"github.com/RocksonZeta/wrap/utils/sutil"
	"github.com/RocksonZeta/wrap/utils/timeutil"
)

const tableTeamUser = "TeamUser"
const tableTeamUserId = "Id"

var logTeamUsers = localwrap.GetLogger().Fork("itodo/core/dao/teamdao", "TeamUsers")

//TeamUsers
type TeamUsers struct {
	*redised.RedisedMysql
}

type TeamUsersQueryParam struct {
	common.Page
	Name string
}

//Query 根据条件查询
func (s TeamUsers) Query(param TeamUsersQueryParam) ([]table.TeamUser, int64) {
	logTeamUsers.Debug().Func("Query").Interface("param", param).Send()
	wsql := " from TeamUser where 1=1"
	if param.Name != "" {
		param.Name = "%" + param.Name + "%"
		wsql += " and name like :Name"
	}
	ssql := "select *" + wsql + " order by ct desc" + param.Limit()
	csql := "select count(*)" + wsql
	var r []table.TeamUser
	s.Mysql.Select(&r, ssql, param)
	return r, s.Mysql.SelectInt(csql, param)
}

func (s TeamUsers) Get(id int) table.TeamUser {
	logTeamUsers.Debug().Func("Get").Int("id", id).Send()
	var r table.TeamUser
	s.RedisedMysql.Get(&r, tableTeamUser, tableTeamUserId, id)
	return r
}

func (s TeamUsers) List(ids []int) []table.TeamUser {
	logTeamUsers.Debug().Func("List").Ints("ids", ids).Send()
	var r []table.TeamUser
	s.RedisedMysql.List(&r, tableTeamUser, tableTeamUserId, ids)
	return r
}
func (s TeamUsers) ListAsMap(ids []int) map[int]table.TeamUser {
	logTeamUsers.Debug().Func("ListAsMap").Ints("ids", ids).Send()
	l := s.List(ids)
	r := make(map[int]table.TeamUser, len(l))
	for _, v := range l {
		r[v.Id] = v
	}
	return r
}

func (s TeamUsers) Delete(id int) {
	logTeamUsers.Debug().Func("Delete").Int("id", id).Send()
	old := s.Get(id)
	s.Mysql.Delete(tableTeamUser, tableTeamUserId, id)
	s.ClearCache(old)
}

func (s TeamUsers) ClearCache(old table.TeamUser) {
	logTeamUsers.Debug().Func("ClearCache").Int("id", old.Id).Send()
	s.Redis.Del(s.KeyFn(tableTeamUser, old.Id))
	s.Redis.Del(s.KVFn(tableTeamUser, map[string]interface{}{"teamId": old.TeamId, "uid": old.Uid}))
	s.Redis.Del(s.KVFn(tableTeamUser, map[string]interface{}{"uid": old.Uid}))
	s.Redis.Del(s.KVFn(tableTeamUser, map[string]interface{}{"teamId": old.TeamId}))
}
func (s TeamUsers) GetByTeamIdUid(teamId int, uid int) table.TeamUser {
	logTeamUsers.Debug().Func("GetByTeamIdUid").Int("teamId", teamId).Int("uid", uid).Send()
	var r table.TeamUser
	s.RedisedMysql.GetByKvs(&r, tableTeamUser, tableTeamUserId, map[string]interface{}{"teamId": teamId, "uid": uid})
	return r
}
func (s TeamUsers) ListByUid(uid int) []table.TeamUser {
	logTeamUsers.Debug().Func("ListByUid").Int("uid", uid).Send()
	var r []table.TeamUser
	s.RedisedMysql.ListByKvs(&r, tableTeamUser, tableTeamUserId, map[string]interface{}{"uid": uid})
	return r
}
func (s TeamUsers) ListByTeamId(teamId int) []table.TeamUser {
	logTeamUsers.Debug().Func("ListByTeamId").Int("teamId", teamId).Send()
	var r []table.TeamUser
	s.RedisedMysql.ListByKvs(&r, tableTeamUser, tableTeamUserId, map[string]interface{}{"teamId": teamId})
	return r
}

type TeamUsersUpdateParam struct {
	Id   int
	Role int
}

func (s TeamUsers) Update(param TeamUsersUpdateParam) {
	logTeamUsers.Debug().Func("Update").Interface("param", param).Send()
	s.Mysql.Patch(tableTeamUser, tableTeamUserId, param)
	s.Redis.Del(s.KeyFn(tableTeamUser, param.Id))
	// s.ClearCache(s.Get(param.Id)) if no index updated
}

type TeamUsersAddParam struct {
	TeamId int
	Uid    int
	Role   int
}

func (s TeamUsers) Add(param TeamUsersAddParam) table.TeamUser {
	logTeamUsers.Debug().Func("Add").Interface("param", param).Send()
	var t table.TeamUser
	sutil.Copy(param, &t)
	t.Ct = timeutil.Now()
	s.Mysql.AddTable(t)
	s.Mysql.Insert(&t)
	s.ClearCache(t)
	return t
}
